package org.yinxue.swing.im.ui;

import com.google.common.eventbus.Subscribe;
import lombok.Data;
import org.yinxue.swing.core.panel.JTextAreaWithJScroll;
import org.yinxue.swing.core.standard.YxPanel;
import org.yinxue.swing.core.standard.YxStructure;
import org.yinxue.swing.im.domain.event.ClientListGetEvent;
import org.yinxue.swing.im.domain.event.EventPublisher;
import org.yinxue.swing.im.domain.event.MessageReceiveEvent;
import org.yinxue.swing.im.domain.event.MessageSendEvent;
import reactor.core.publisher.FluxSink;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.List;
import java.util.*;

import static org.yinxue.swing.core.util.SwingUtils.*;

@Data
public class IMClientPanel extends YxPanel implements YxStructure {

    private FluxSink fluxSink;
    private JPanel jPanel_left_button;
    private JPanel jPanel_left;
    private JTextAreaWithJScroll jPanel_right_top;
    private JTextAreaWithJScroll jPanel_right_down;
    private JButton jButton_send;
    private JTree jTree;

    @Override
    public void init() {
        jPanel_left_button = ofJPanelWithBorderSize(64, 512);
        jPanel_left = ofJPanelWithBorderSize(256, 512);
        jPanel_right_top = ofJTextAreaWithJScroll();
        jPanel_right_down = ofJTextAreaWithJScroll();
        jButton_send = ofJButton("发送");
        jTree = ofJTree();
        GridBagConstraints g1 = ofGridBagConstraints();
        jPanel_left.add(new JScrollPane(jTree), g1);
    }

    @Override
    public void position() {
        GridBagConstraints g1 = ofGridBagConstraints();
        g1.weightx = 0;
        g1.gridheight = 2;
        add(jPanel_left_button, g1);

        g1.gridx = 1;
        add(jPanel_left, g1);

        g1.gridheight = 1;
        g1.gridx = 2;
        g1.weightx = 1;
        g1.weighty = 1;
        add(jPanel_right_top, g1);

        g1.gridy = 1;
        g1.weighty = 0.5;
        add(jPanel_right_down, g1);
    }

    public String currentIp = "";

    @Override
    public void action() {
        jTree.addTreeSelectionListener(e -> {
            String selectIp = e.getNewLeadSelectionPath().getLastPathComponent().toString();
            currentIp = selectIp;
            System.out.println("当前选择ip:" + currentIp);
        });

        jPanel_right_down.getTextArea().addKeyListener(new KeyListener() {
            @Override
            public void keyTyped(KeyEvent e) {

            }

            @Override
            public void keyPressed(KeyEvent e) {
                System.out.println("modifies:" + e.getModifiers());
                System.out.println("keyCode:" + e.getKeyCode());
                if (e.getModifiers() == 0 && e.getKeyCode() == 10) {
                    String message = "MSG;" + currentIp + ";" + jPanel_right_down.getText() + "\n";
                    EventPublisher.instance().publish(new MessageSendEvent(message));
                    jPanel_right_top.appendAndRefresh("我:" + jPanel_right_down.getText() + "\n");
                }
            }

            @Override
            public void keyReleased(KeyEvent e) {
                if (e.getModifiers() == 0 && e.getKeyCode() == 10) {
                    jPanel_right_down.setText("");
                    jPanel_right_down.refreshCaretPosition();
                }
            }
        });
    }

    @Override
    public void destory() {

    }

    private Set<String> ipSet = new HashSet<>();

    @Subscribe
    public void listen(ClientListGetEvent event) {
        String[] ips = event.getClients();
        DefaultMutableTreeNode root = (DefaultMutableTreeNode) (jTree.getModel()).getRoot();
        Enumeration enumeration = root.children();
        List<String> oldIps = new ArrayList<>();
        while (enumeration.hasMoreElements()) {
            oldIps.add(enumeration.nextElement().toString());
        }
        Arrays.stream(ips).filter(ip -> !oldIps.contains(ip)).forEach(s -> {
            ((DefaultTreeModel) jTree.getModel()).insertNodeInto(new DefaultMutableTreeNode(s), root, 0);
            jTree.expandRow(0);
        });
        System.out.println("事件:" + event.getClass().getName());
    }

    @Subscribe
    public void listen(MessageReceiveEvent event) {
        jPanel_right_top.append(event.getMessage());
    }
}
